package com.ruoyi.lifecycle.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.lifecycle.domain.vo.TDistributorVo;
import com.ruoyi.lifecycle.domain.bo.TDistributorBo;
import com.ruoyi.lifecycle.service.ITDistributorService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 销售商信息控制器
 * 前端访问路由地址为:/lifecycle/distributor
 *
 * @author chq
 * @date 2025-06-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/distributor")
public class TDistributorController extends BaseController {

    private final ITDistributorService iTDistributorService;

    /**
     * 查询销售商信息列表
     */
    @SaCheckPermission("lifecycle:distributor:list")
    @GetMapping("/list")
    public TableDataInfo<TDistributorVo> list(TDistributorBo bo, PageQuery pageQuery) {
        return iTDistributorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售商信息列表
     */
    @SaCheckPermission("lifecycle:distributor:export")
    @Log(title = "销售商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TDistributorBo bo, HttpServletResponse response) {
        List<TDistributorVo> list = iTDistributorService.queryList(bo);
        ExcelUtil.exportExcel(list, "销售商信息", TDistributorVo.class, response);
    }

    /**
     * 获取销售商信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lifecycle:distributor:query")
    @GetMapping("/{id}")
    public R<TDistributorVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iTDistributorService.queryById(id));
    }

    /**
     * 新增销售商信息
     */
    @SaCheckPermission("lifecycle:distributor:add")
    @Log(title = "销售商信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TDistributorBo bo) {
        return toAjax(iTDistributorService.insertByBo(bo));
    }

    /**
     * 修改销售商信息
     */
    @SaCheckPermission("lifecycle:distributor:edit")
    @Log(title = "销售商信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TDistributorBo bo) {
        return toAjax(iTDistributorService.updateByBo(bo));
    }

    /**
     * 删除销售商信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lifecycle:distributor:remove")
    @Log(title = "销售商信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iTDistributorService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
